package chemtools_logica.BDL.SFBean;

import chemtools_logica.BDL.IL.BDL_SFFormulaLocal;
import chemtools_logica.BDL.IR.BDL_SFFormulaRemote;

import chemtools_logica.Beans.FormulaBean;

import chemtools_logica.Entidades.Formula;

import chemtools_logica.Entidades.Ordenproduccion;

import chemtools_logica.Entidades.Transformacion;

import java.text.SimpleDateFormat;

import java.util.List;

import javax.annotation.Resource;

import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name = "BDL_SFFormula", mappedName = "map-BDL_SFFormula")
public class BDL_SFFormulaBean implements BDL_SFFormulaRemote, BDL_SFFormulaLocal {
    @Resource
    SessionContext sessionContext;
    @PersistenceContext(unitName = "Chemtools_Logica")
    private EntityManager em;

    public BDL_SFFormulaBean() {
    }

    public Formula persistFormula(Formula formula) {
        em.persist(formula);
        return formula;
    }

    public Formula mergeFormula(Formula formula) {
        return em.merge(formula);
    }

    public void removeFormula(Formula formula) {
        formula = em.find(Formula.class, formula.getIdFormula());
        em.remove(formula);
    }

    /** <code>select o from Formula o</code> */
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public List<Formula> getFormulaFindAll() {
        return em.createNamedQuery("Formula.findAll", Formula.class).getResultList();
    }
    
    public int insertFormula(Formula formula){
        em.persist(formula);
        em.flush();
        int id = formula.getIdFormula();
        return id;
    }

    public List<Formula> getFormulaFindBean(FormulaBean buscaFormulaBean) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String ejbQuery = "select u.* FROM formula as u where 1=1 ";
        
        if (buscaFormulaBean.getIdFormula()!=0) {
            ejbQuery += " and u.idFormula = " + buscaFormulaBean.getIdFormula();
        }
        
        if (buscaFormulaBean.getDescripcion()!=null && !buscaFormulaBean.getDescripcion().equals("")) {
            ejbQuery += " and u.descripcion like '%" + buscaFormulaBean.getDescripcion() + "%'";
        }
        
        if (buscaFormulaBean.getNombre_Creador()!=null) {
            if(!buscaFormulaBean.getNombre_Creador().equals("")){
                ejbQuery += " and u.nombre_Creador like '%" + buscaFormulaBean.getNombre_Creador() + "%'";
            }
        }
        
        if (buscaFormulaBean.getApellido_Creador()!=null && !buscaFormulaBean.getApellido_Creador().equals("")) {
            ejbQuery += " and u.apellido_Creador like '%" + buscaFormulaBean.getApellido_Creador() + "%'";
        }
        
       
            
            if(buscaFormulaBean.getFecha_CreacionDesde()!= null){
                ejbQuery += "AND u.fecha_Creacion >= '"+sdf.format(buscaFormulaBean.getFecha_CreacionDesde())+"' ";
            }
            
            if(buscaFormulaBean.getFecha_CreacionHasta() != null){
                ejbQuery += "AND u.fecha_Creacion <= '"+sdf.format(buscaFormulaBean.getFecha_CreacionHasta())+"' ";
            }
            
      
        if (buscaFormulaBean.getEstado()>=0) {
            ejbQuery += " and u.estado=" + buscaFormulaBean.getEstado();
        }
        System.out.println(ejbQuery);
        Query query = em.createNativeQuery(ejbQuery, Formula.class); 
        return query.getResultList();
    }
    
    public List<Transformacion> transformacionesxFormula(int idFormula){
            String ejbQuery = "select u FROM Transformacion as u where u.formula.idFormula="+idFormula;
           return em.createQuery(ejbQuery, Transformacion.class).getResultList();
        }
    
}
